#include "factory_api.h"
#include "config_factory.h"
#include <rtthread.h>

#define DBG_ENABLE
#define DBG_SECTION_NAME "factory_config"
#include "uc_log.h"

factory_test_t factory_test_data[MAX_TEST_PROJECT] = {0};

char *factory_get_key(factory_test_type_e test_type)
{
    char *g_factory_key[] = {
        "w0",
        "e0",
        "pp",
        "bt",
        "485",
        "sd",
        "exflash",
        "rtc",
        "key",
        "led",
        "wiota",
        "gps",
        "sync",
        "mode",
    };

    return g_factory_key[test_type];
}

void factory_data_init(void)
{
    if (uc_read_factory_data(factory_test_data, MAX_TEST_PROJECT))
    {
        factory_data_save();
    }
}

void factory_data_save(void)
{
    if (uc_write_factory_data(factory_test_data, MAX_TEST_PROJECT))
    {
        TRACE_E("uc_write_factory_data");
    }
}

factory_test_t *factory_get_data(void)
{
    return factory_test_data;
}

// 测试结果备注
int factory_test_result(factory_test_type_e test_type, char *name, factory_test_state_e state, char *remark)
{
    // 设置数据
    factory_test_data[test_type].state = state;
    if (NULL != remark)
    {
        rt_memset(factory_test_data[test_type].remark, 0, sizeof(factory_test_data[test_type].remark));
        rt_memcpy(factory_test_data[test_type].remark, remark, rt_strlen(remark));
    }

    if (NULL != name)
    {
        rt_memcpy(factory_test_data[test_type].name, name, rt_strlen(name));
    }

    factory_data_save();

    return 0;
}

int factory_get_result(factory_test_type_e test_type, char *name, factory_test_state_e *state, char *remark)
{
    if (NULL != name)
    {
        rt_memcpy(name, factory_test_data[test_type].name, rt_strlen(factory_test_data[test_type].name));
    }

    if (NULL != state)
    {
        *state = factory_test_data[test_type].state;
    }

    if (NULL != remark)
    {
        rt_memcpy(remark, factory_test_data[test_type].remark, rt_strlen(factory_test_data[test_type].remark));
    }

    return 0;
}
